package es.index.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author zhoujin 对文件进行本地存储
 */
public class FileStorage {
	public FileStorage() {
	}

	public void storage(String path, byte[] bytes, int offset) {
		File file = new File(path);
		if (!file.exists()) {
			File dir = new File(new String(path.substring(0,
					path.lastIndexOf(File.separatorChar))));
			if (!dir.exists()) {
				dir.mkdirs();
			}
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// LockFile _lockFile = new LockFile(file);
		// _lockFile.writeBytes(bytes, offset);
		// _lockFile.release();
		// _lockFile.close();
		// _lockFile = null;

		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(file, "rw");
			raf.seek(offset);
			raf.write(bytes);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (Exception e) {
			}
		}

	}
}